package swg.shape;

import java.util.ArrayList;

public class Polyline implements IswgShape
{
	protected String style="";
	protected ArrayList<Point> points = new ArrayList<Point>();
	protected boolean caps;
		
	public Polyline(String style, ArrayList<Point> points)
	{
		this.style = style;
		this.points = points;
	}


	public String getStyle()
	{
		return style;
	}


	public void setStyle(String style)
	{
		this.style = style;
	}


	public ArrayList<Point> getPoints()
	{
		return points;
	}


	public void setPoints(ArrayList<Point> points)
	{
		this.points = points;
	}

	
	public boolean isCaps()
	{
		return caps;
	}


	public void setCaps(boolean caps)
	{
		this.caps = caps;
	}


	@Override
	public String getSWGCode()
	{
		StringBuffer svg = new StringBuffer("<polyline points=\"");
		for(Point p: points){
			svg.append(p.getCoordinates());
			
		}
		svg.append("\" style=\"");
		svg.append(style);
		svg.append("\" />");
		if (caps){
			for(Point p: points) {
				p.setStyle("fill:red");
				svg.append(p.getSWGCode());
			}
		}
		return svg.toString();
	}


	@Override
	public int getX()
	{
		int xx=0;
		for(Point p : points){
			xx=xx+p.getX();
		}
		return xx/points.size();
	}


	@Override
	public int getY()
	{

		int yy=0;
		for(Point p : points){
			yy=yy+p.getY();
		}
		return yy/points.size();
	}


	@Override
	public int getR()
	{
		// TODO Auto-generated method stub
		return 0;
	}


	public boolean intersect(Point p4, double gap)
	{
		gap=-gap;
		if (points.size()<3)
			return false;
		Point p3=points.get(points.size()-1);
		if((p4.getX()-p3.getX())==0){

			return true;
		}
		double a1=(p4.getY()-p3.getY())*1./(p4.getX()-p3.getX())*1.;
		double b1=p3.getY()*1.0-a1*p3.getX()*1.0;
	

		
		for(int i =0; i<points.size()-2;i++){
			
			//System.out.println(i);
			Point p1=points.get(i);
			Point p2=points.get(i+1);
			
			double a2=(p2.getY()-p1.getY())*1.0/(p2.getX()-p1.getX())*1.0;
			double b2=p1.getY()*1.0-a2*p1.getX()*1.0;
			
			double xx=(b2-b1)/(a1-a2);
			double yy=a1*22+b1;

			double px1=p1.getX()*1.;
			double px2=p2.getX()*1.;
			
			double py1=p1.getY()*1.;
			double py2=p2.getY()*1.;
			boolean resultX = true;
			boolean resultY = true;

			if(px1>px2)
			{
				if(px2+gap<xx && xx<px1-gap){
					resultX=false;
				}
			}
			else
			{
				if(px1+gap<xx && xx<px2-gap){
					resultX=false;
				}
			}

			

			
			if(py1>py2)
			{
				if(py2+gap<yy && yy<py1-gap){
					resultY=false;
				}
			}
			else
			{
				if(py1+gap<yy && yy<py2-gap){
					resultY=false;
				}
			}

			if(!(resultX && resultY))
				return true;
			
		}
		return false;
	}


	public static Polyline getBox(double xMin, double xMax, double yMin, double yMax, String boxStyle)
	{
		ArrayList<Point> res = new ArrayList<Point>();	
		Point a = new Point(xMin,yMin);	
		Point b = new Point(xMin,yMax);
		Point c = new Point(xMax,yMax);
		Point d = new Point(xMax,yMin);
		res.add(a);
		res.add(b);
		res.add(c);
		res.add(d);
		res.add(a);
		return new Polyline(boxStyle, res);
	}

}
